<?php

namespace app\admin\model;

use think\facade\Session;
use think\Model;

class AdminModel extends Model
{
    protected $table = 'admins';

    protected $hidden = ['password'];

    protected $autoWriteTimestamp = true;

    public function role()
    {
        return $this->hasOne(RoleModel::class, 'id', 'role_id')->field('id,name');
    }

    public static function loginPost($request, $ip)
    {
        $admin = AdminModel::where('username', $request['username'])->find();
        // 验证管理员是否存在
        if (!$admin) {
            LoginLogModel::addLog($request['username'], 1, $request['password'], $ip, 0);
            return baseResponse(1, '用户不存在');
        }
        // 验证管理员密码是否正确
        if (!strVery($request['password'], $admin['password'])) {
            LoginLogModel::addLog($request['username'], 1, $request['password'], $ip, 0);
            return baseResponse(1, '用户密码不正确');
        }
        // 验证管理员是否被禁用
        if ($admin['status'] == 0) {
            LoginLogModel::addLog($request['username'], 1, $request['password'], $ip, 0);
            return baseResponse(1, '当前用户已被禁用');
        }
        // 登陆成功后钩子
        $admin->login_num += 1;
        $admin->last_login_time = time();
        $admin->last_login_ip = $ip;
        $admin->save();
        LoginLogModel::addLog($request['username'], 1, $request['password'], $ip, 1);
        // 设置Session
        Session::set('admin', $admin['id']);
        return baseResponse(0, '登录成功');
    }

    public static function lists($request)
    {
        $lists = AdminModel::with('role')->order('id asc');
        if (!isset($request['page']) || empty($request['page'])) {
            $request['page'] = "1";
        }
        if (!isset($request['limit']) || empty($request['limit'])) {
            $request['limit'] = "10";
        }
        if (isset($request['username']) && $request['username'] != '') {
            $lists = $lists->where("username", "like", "%" . $request['username'] . "%");
        }
        if (isset($request['mobile']) && $request['mobile'] != '') {
            $lists = $lists->where("mobile", "like", "%" . $request['mobile'] . "%");
        }
        if (isset($request['status']) && $request['status'] != '') {
            $lists = $lists->where("status", $request['status']);
        }
        $data['count'] = $lists->count();
        $data['lists'] = $lists->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->select();
        return baseResponse(0, '获取管理员列表成功', $data);
    }

    public static function createSubmit($request)
    {
        $admin = AdminModel::where('username', $request['username'])->find();
        $role = RoleModel::where('id', $request['role_id'])->where('status', 1)->find();
        if ($admin || !$role) {
            return baseResponse(1, '管理员账号重复或角色选择错误');
        } else {
            AdminModel::create([
                'role_id' => $request['role_id'],
                'username' => $request['username'],
                'password' => strHash($request['password']),
                'mobile' => $request['mobile'],
                'email' => $request['email'],
                'status' => $request['status'],
            ]);
            return baseResponse(0, '管理员创建成功');
        }
    }

    public static function updateSubmit($request, $id)
    {
        $admin = AdminModel::where('username', $request['username'])->where('id', '<>', $id)->find();
        $role = RoleModel::where('id', $request['role_id'])->where('status', 1)->find();
        if ($admin || !$role) {
            return baseResponse(1, '管理员账号重复或角色选择错误');
        } else {
            $data = [
                'id' => $id,
                'role_id' => $request['role_id'],
                'username' => $request['username'],
                'mobile' => $request['mobile'],
                'email' => $request['email'],
                'status' => $request['status'],
            ];
            if (isset($request['password']) && !empty($request['password'])) {
                $data['password'] = strHash($request['password']);
            }
            AdminModel::update($data);
            return baseResponse(0, '管理员编辑成功');
        }
    }
}
